<template>
  <q-item
    clickable
    tag='a'
    target='_blank'
    @click='routeToLink(link)'
  >
    <q-item-section
      v-if='icon'
      avatar
    >
      <q-icon :name='icon' />
    </q-item-section>
    <q-item-section>
      <q-item-label>{{ title }}</q-item-label>
      <q-item-label caption>
        {{ caption }}
      </q-item-label>
    </q-item-section>
  </q-item>
</template>
<script lang='ts'>
import { defineComponent } from 'vue';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: 'EssentialLink',
  props: {
    title: {
      type: String,
      required: true
    },
    caption: {
      type: String,
      default: ''
    },
    link: {
      type: String,
      default: '#'
    },
    icon: {
      type: String,
      default: ''
    }
  },
  setup() {
    const router = useRouter();

    function routeToLink(link: string): void {
      void router.push('/');
      setTimeout(() => {
        void router.push('/list?entityCode=' + link);
      }, 10);
    }

    return {
      routeToLink
    };
  }
});
</script>
